//https://leetcode.cn/problems/n-ary-tree-level-order-traversal/
package codeRandomThoughts.Test429N叉树的层序遍历;

import codeRandomThoughts.utils.Node;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class Solution {
    public List<List<Integer>> levelOrder(Node root) {
        List<Integer> result = new ArrayList<>();
        List<List<Integer>> results = new ArrayList<>();
        if (root==null){
            return results;
        }
        Queue<Node> queue = new LinkedList<>();
        Node node = root;
        queue.offer(node);
        int size = 0;//每层的结点数

        while (!queue.isEmpty()) {
            size = queue.size();
            while (size > 0) {
                node = queue.poll();
                result.add(node.val);
                for (int i = 0; i < node.children.size(); i++) {
                    if (node.children.get(i) != null) {
                        queue.add(node.children.get(i));
                    }
                }
                size--;
            }
            results.add(result);
            result = new ArrayList<>();
        }
        return results;
    }
}
